/*
 * NewAccountDialog.java
 *
 * Created on 2011-5-14, 21:47:37
 */
package banksimulator.ui;

import banksimulator.bank.AbstractAccount;
import banksimulator.bank.AccountFactory;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.WindowConstants;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

/**
 * 新建帐户对话框，用来输入、显示新建帐户的信息。
 * <p>使用此对话框可以方便的新建一个帐户，如使用下列代码:
 * <pre>
 * NewAccountDialog dialog = new NewAccountDialog(parent, true, factory);
 * account = dialog.getAccount();</pre>
 * 其中，parent为对话框的父窗口，factory为具体某种AbstractAccount的工厂。
 * @author Rajab
 */
public class NewAccountDialog extends javax.swing.JDialog {

    //新建的帐户
    private AbstractAccount account = null;
    //对话框父窗口
    private Frame parent = null;
    //帐户工厂
    private AccountFactory accountFactory;

    /** 建立一个新的新建帐户对话框。
     * @param modal 是否是模式对话框
     */
    public NewAccountDialog(java.awt.Frame parent, boolean modal, AccountFactory accountFactory) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        txtUserID.setText(accountFactory.nextAccountID());
        this.accountFactory = accountFactory;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtUserID = new JTextField();
        lblUserID = new JLabel();
        lblUserName = new JLabel();
        txtUserName = new JTextField();
        lblBalance = new JLabel();
        txtBalance = new JFormattedTextField();
        btnOK = new JButton();
        btnCancel = new JButton();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("新建帐户");
        setResizable(false);

        txtUserID.setEditable(false);
        txtUserID.setName("txtUserID"); // NOI18N

        lblUserID.setText("帐户ID：");
        lblUserID.setName("lblUserID"); // NOI18N

        lblUserName.setText("帐户名：");
        lblUserName.setName("lblUserName"); // NOI18N

        txtUserName.setName("txtUserName"); // NOI18N

        lblBalance.setText("余额：");
        lblBalance.setName("lblBalance"); // NOI18N

        txtBalance.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#,##0.00"))));
        txtBalance.setName("txtBalance"); // NOI18N
        ((NumberFormatter)txtBalance.getFormatter()).setValueClass(Double.class);
        txtBalance.setValue(0.0);

        btnOK.setText("确定");
        btnOK.setName("btnOK"); // NOI18N
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setText("取消");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
                    .addComponent(lblUserID, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblUserName, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblBalance, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                    .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnOK)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(btnCancel))
                    .addComponent(txtUserID, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                    .addComponent(txtUserName, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                    .addComponent(txtBalance, GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblUserID)
                    .addComponent(txtUserID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblUserName)
                    .addComponent(txtUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblBalance)
                    .addComponent(txtBalance, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnOK))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** 按下OK */
    private void btnOKActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        createAccount();
        close();
    }

    /** 建立新帐户 */
    private void createAccount() {
        double balance = ((Double) txtBalance.getValue()).doubleValue();
        account = accountFactory.create(txtUserName.getText(), balance);
    }//GEN-LAST:event_btnOKActionPerformed

    /** 按下取消 */
    private void btnCancelActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        close();
    }//GEN-LAST:event_btnCancelActionPerformed

    /**
     * 关闭对话框并释放资源。
     */
    private void close() {
        setVisible(false);
        dispose();
    }

    /**
     * 显示对话框并且根据输入返回新建后的对话框。
     * <p><b>注意:</b>当按下OK按钮时返回的是新建的帐户，若按下的是取消则返回null。
     * @return 按下OK按钮时返回新建帐户，按下取消返回null
     */
    public AbstractAccount getAccount() {
        setLocationRelativeTo(parent);
        if (account != null) {
            txtUserID.setText(accountFactory.nextAccountID());
            txtUserName.setText("");
            txtBalance.setValue(new Double(0.0));
        }
        setVisible(true);
        return account;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JButton btnCancel;
    private JButton btnOK;
    private JLabel lblBalance;
    private JLabel lblUserID;
    private JLabel lblUserName;
    private JFormattedTextField txtBalance;
    private JTextField txtUserID;
    private JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
